<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\store\controller;

class AdminStore extends \app\AdminRest
{
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
    }
    public function storeList()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", ">", -1];
        if(!empty($input["title"])) {
            $dis[] = ["title", "like", "%" . $input["title"] . "%"];
        }
        if($this->_user["is_admin"] == 0) {
            $dis[] = ["admin_id", "=", $this->_user["admin_id"]];
        }
        $store_model = new \app\store\model\StoreList();
        $data = $store_model->dataList($dis, $input["limit"]);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                if(empty($v["admin_id"])) {
                    $v["status"] = 0;
                }
            }
        }
        return $this->success($data);
    }
    public function storeSelect()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        if(!empty($input["title"])) {
            $dis[] = ["title", "like", "%" . $input["title"] . "%"];
        }
        if($this->_user["is_admin"] == 0) {
            $dis[] = ["admin_id", "=", $this->_user["id"]];
        }
        if(!empty($input["admin_id"])) {
            $dis[] = ["admin_id", "=", $input["admin_id"]];
        }
        $store_model = new \app\store\model\StoreList();
        $data = $store_model->where($dis)->select()->toArray();
        return $this->success($data);
    }
    public function storeAdd()
    {
        $input = $this->_input;
        $input["uniacid"] = $this->_uniacid;
        $input["total_num"] = $input["order_num"];
        $store_model = new \app\store\model\StoreList();
        if(isset($input["cate_id"])) {
            $cate_id = $input["cate_id"];
            unset($input["cate_id"]);
        }
        $res = $store_model->dataAdd($input);
        $id = $store_model->getLastInsID();
        if(!empty($cate_id)) {
            foreach ($cate_id as $k => $value) {
                $insert[$k] = ["uniacid" => $this->_uniacid, "store_id" => $id, "cate_id" => $value];
            }
            $cate_model = new \app\store\model\CateConnect();
            $cate_model->saveAll($insert);
        }
        return $this->success($res, 200, $id);
    }
    public function storeInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $store_model = new \app\store\model\StoreList();
        $res = $store_model->dataInfo($dis);
        $cate_model = new \app\store\model\CateConnect();
        $res["cate_id"] = $cate_model->where(["store_id" => $input["id"]])->column("cate_id");
        $res["cate_id"] = array_values($res["cate_id"]);
        return $this->success($res);
    }
    public function storeUpdate()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        $store_model = new \app\store\model\StoreList();
        $data = $store_model->dataInfo($dis);
        if(isset($input["status"]) && $input["status"] == 1 && empty($data["admin_id"])) {
            $this->errorMsg("未绑定代理商");
        }
        if(isset($input["order_num"])) {
            $input["total_num"] = $input["order_num"] + $data["true_order_num"];
        }
        $cate_model = new \app\store\model\CateConnect();
        if(isset($input["cate_id"])) {
            $cate_id = $input["cate_id"];
            $cate_model->where(["store_id" => $input["id"]])->delete();
            unset($input["cate_id"]);
        }
        $res = $store_model->dataUpdate($dis, $input);
        if(!empty($cate_id)) {
            foreach ($cate_id as $k => $value) {
                $insert[$k] = ["uniacid" => $this->_uniacid, "store_id" => $input["id"], "cate_id" => $value];
            }
            $cate_model->saveAll($insert);
        }
        return $this->success($res);
    }
    public function cateList()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", ">", -1];
        if(!empty($input["title"])) {
            $dis[] = ["title", "like", "%" . $input["title"] . "%"];
        }
        $cate_model = new \app\store\model\StoreCate();
        $data = $cate_model->dataList($dis, $input["limit"]);
        return $this->success($data);
    }
    public function cateSelect()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        if(!empty($input["title"])) {
            $dis[] = ["title", "like", "%" . $input["title"] . "%"];
        }
        $cate_model = new \app\store\model\StoreCate();
        $data = $cate_model->where($dis)->order("top desc")->select()->toArray();
        return $this->success($data);
    }
    public function cateAdd()
    {
        $input = $this->_input;
        $input["uniacid"] = $this->_uniacid;
        $cate_model = new \app\store\model\StoreCate();
        $data = $cate_model->dataAdd($input);
        return $this->success($data);
    }
    public function cateUpdate()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        $cate_model = new \app\store\model\StoreCate();
        $res = $cate_model->dataUpdate($dis, $input);
        return $this->success($res);
    }
    public function cateInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $cate_model = new \app\store\model\StoreCate();
        $res = $cate_model->dataInfo($dis);
        return $this->success($res);
    }
}

?>